package com.jnshu.tools;

import lombok.extern.apachecommons.CommonsLog;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.net.URLDecoder;

/**
 * @author xmp
 * @date 2019/8/7 3:10
 */
@CommonsLog
public class HeadUtil {
    public static String getHeadValue(String headKey) {
        try {
            // 这种方式获取的HttpServletRequest是线程安全的,然后获取到请求头
            String valueEncode = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes ()).getRequest ().getHeader (headKey);

            if (valueEncode == null) {
                return null;
            }

            String value = URLDecoder.decode (valueEncode, "UTF-8");//解码
            return value;
        } catch ( Exception e ) {
            return null;
        }
    }
}
